package com.yanzhengwu.boot.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.yanzhengwu.boot.core.common.annotation.MultiDataSourceTransactional;
import com.yanzhengwu.boot.core.common.annotation.TransactionMulti;
import com.yanzhengwu.boot.core.datasource.DynamicDataSource;
import com.yanzhengwu.boot.entity.Source;
import com.yanzhengwu.boot.mapper.SourceMapper;
import com.yanzhengwu.boot.service.ISourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: yanzhengwu
 * @Date: 2020/6/8 10:49
 * @Description:
 */
@Service
public class SourceServiceImpl extends ServiceImpl<SourceMapper, Source> implements ISourceService {

    @Resource
    private SourceMapper sourceMapper;

    @Resource
    private DynamicDataSource dynamicDataSource;

    @Override
    //@DataSource(name = DataSourcesType.SLAVE)
    public List<Source> index() {
        List<Source> list = sourceMapper.list();
        System.out.println(list);
        return list;

    }

    //@DataSource(name = DataSourcesType.SLAVE)
    @Override
    @TransactionMulti
    public String insert(Source source) {
        Integer mRow = 0;
        Integer sRow = 0;
        Integer oRow = 0;

        source.setId(IdWorker.getId());
        sRow = sourceMapper.insertDataSlave(source);

        source.setId(IdWorker.getId());
        mRow = sourceMapper.insertDataMaster(source);
        //if (mRow==1) throw new RuntimeException("抛异常");

        source.setId(IdWorker.getId());
        oRow = sourceMapper.insertDataOther(source);


        String res = "mRow: " + mRow + "   " + "sRow: " + sRow + "oRow: " + oRow;
        System.out.println(res);
        return res;
    }

}